import {
    deleteMsgById,
    deleteMsgs,
    getMsgById,
    getUnReadCount,
    queryMessages,
    updateMsgReady
} from "../../services/workbench/messageCenter";
import Constants from '../../utils/Constants'
import {message} from "antd";

const R = require('ramda');
export default {
    namespace: 'messageCenter',
    state: {
        page: {},
        list: [],
        messageDetail: {},
        unReadCount: 0
    },
    reducers: {
        /**
         * 刷新state
         *
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 获取消息详情
         *
         * @param call
         * @param put
         * @param select
         */
        *getMsgById({ payload }, { call, put, select }) {
            const res = yield call(getMsgById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        messageDetail: res.data.content,
                    }
                });
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 分页查询消息列表
         *
         * @param call
         * @param put
         * @param select
         */
        *queryMessages({ payload }, { call, put, select }) {
            const res = yield call(queryMessages, { ...R.omit(['onSuccess', 'onFail'])(payload), status: '1' });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
                if (payload.onSuccess) payload.onSuccess(res.data.content.list);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 更新消息为已读
         *
         * @param call
         * @param put
         * @param select
         */
        *updateMsgReady({ payload }, { call, put, select }) {
            const res = yield call(updateMsgReady, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 查看未读消息总数
         *
         * @param call
         * @param put
         * @param select
         */
        *getUnReadCount({ payload }, { call, put, select }) {
            const res = yield call(getUnReadCount);
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        unReadCount: res.data.content,
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 批量删除
         *
         * @param call
         * @param put
         * @param select
         */
        *deleteMsgs({ payload }, { call, put, select }) {
            const res = yield call(deleteMsgs, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("删除成功");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 删除消息
         *
         * @param call
         * @param put
         * @param select
         */
        *deleteMsgById({ payload }, { call, put, select }) {
            const res = yield call(deleteMsgById, { id: payload.id });
            if (res && res.data.resultCode === "ok") {
                message.info("删除成功");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};